#include "BlockXmlHandler.h"
#include <list>

namespace xpm {
	void BlockXmlHandler::Parse() {
		//parse the attributes
		//if(map && map->getLength() > 0) {
		//	IidXmlHandler iidXmlHandler(_doc, map, _voc);
		//	iidXmlHandler.Parse();
		//	StructureXmlHandler strcXmlHandler(_doc, map, _voc);
		//	strcXmlHandler.Parse();
		//	InterpretationXmlHandler interXmlHandler(_doc, map, _voc);
		//	interXmlHandler.Parse();
		//	//to do: parse the attributes for context
		//	VocabularyContext* context = _voc->GetVocabularyContext();	
		//	VocabularyContextXmlHandler vocContextHandler(_doc, map, _voc, context);
		//	vocContextHandler.Parse();
		//}
		////parse the chindnode
		//DOMNodeList* nodeList = xmlRoot->getChildNodes();
		//for(XMLSize_t i = 0; i < nodeList->getLength(); ++i) {
		//	node = nodeList->item(i);
		//	if(node->getAttributes()) {
		//	   Term* term = _voc->CreateTerm();
		//	   TermXmlHandler termHandler(_doc, node, term);
		//	   termHandler.Parse();
		//	}
		//}
	}

	void BlockXmlHandler::Serialize() {		
		DOMNode* node = NULL;
		//to do: parse the attributes		
		DOMNamedNodeMap* attrs = _node->getAttributes();

		XMLCh tempStr[500];
		char temp[500];
		DOMAttr* attr;
		//seralize the position attribute
		XMLString::transcode("pos", tempStr, 499);
		attr = _doc->createAttribute(tempStr);
		XMLString::transcode(_itoa(_block->position(), temp, 10), tempStr, 499);
		attr->setValue(tempStr);
		attrs->setNamedItem(attr);		
		//seralize the terms in the block
		std::list<TermRange> terms = _block->GetTerms();
		DOMElement* nodeElement = NULL;
		XMLCh* termName = XMLString::transcode("Term");		
		for(std::list<TermRange>::iterator it = terms.begin(); it != terms.end(); ++it) {
			nodeElement = _doc->createElement(termName);
			_node->appendChild(nodeElement);
			attrs = nodeElement->getAttributes();
			//parse attributes for terms
			//seralize the position attribute
			XMLString::transcode("pos", tempStr, 499);
			attr = _doc->createAttribute(tempStr);
			XMLString::transcode(_itoa(it->posInBlock(), temp, 10), tempStr, 499);
			attr->setValue(tempStr);
			attrs->setNamedItem(attr);
			//seralize the length attribute
			//seralize the iid
			XMLString::transcode("iid", tempStr, 499);
			attr = _doc->createAttribute(tempStr);
			XMLString::transcode(it->iid().c_str(), tempStr, 499);
			attr->setValue(tempStr);
			attrs->setNamedItem(attr);
			//seralize the term text
			//XMLString::transcode(it->text().c_str(), tempStr, 499);
			nodeElement->setTextContent(it->text().c_str());
		}
		XMLString::release(&termName);
	}
}